package SecondSaturday;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class minorityFind extends JFrame {	
	private JPanel contentPane;
	private JLabel label;
	private JTextField numIn1;
	private JLabel label_1;
	private JTextField numIn2;
	private JButton btnStart;
	private JScrollPane scrollPane;
	private JTextArea result;
	
	int min=0,max=0;
	int maxx=0;
	int j=0,count=0;
	int sta,end;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					minorityFind frame = new minorityFind();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public minorityFind() {
		setTitle("Minority Find");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 477, 435);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLabel());
		contentPane.add(getNumIn1());
		contentPane.add(getLabel_1());
		contentPane.add(getNumIn2());
		contentPane.add(getBtnStart());
		contentPane.add(getScrollPane());
		
		
		
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("범위");
			label.setFont(new Font("굴림", Font.PLAIN, 15));
			label.setBounds(12, 10, 36, 15);
		}
		return label;
	}
	private JTextField getNumIn1() {
		if (numIn1 == null) {
			numIn1 = new JTextField();
			numIn1.setBounds(49, 7, 116, 21);
			numIn1.setColumns(10);
		}
		return numIn1;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("~");
			label_1.setFont(new Font("굴림", Font.PLAIN, 15));
			label_1.setBounds(187, 10, 26, 15);
		}
		return label_1;
	}
	private JTextField getNumIn2() {
		if (numIn2 == null) {
			numIn2 = new JTextField();
			numIn2.setBounds(219, 7, 116, 21);
			numIn2.setColumns(10);
		}
		return numIn2;
	}
	private JButton getBtnStart() {
		if (btnStart == null) {
			btnStart = new JButton("Start");
			btnStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					//minor_my(); //내코드
					//minor_tc();   //강사님 코드
					test(); // 
					
				}
			});
			btnStart.setBounds(347, 6, 97, 23);
		}
		return btnStart;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 42, 437, 345);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
			result.setLineWrap(true);
		}
		return result;
	}
	public void minor_my(){ // 내코드
		// SimpleDateFormat
		/*SimpleDateFormat in = new SimpleDateFormat("mm:ss");					
		String sta = in.format(new Date());*/
		
		long stime = 0;
		long etime = 0;
		stime = System.currentTimeMillis();
		
		min=Integer.parseInt(numIn1.getText());
		max=Integer.parseInt(numIn2.getText());
		maxx=(int)max;
		ArrayList<Boolean> primeList;
		if(max<=1){
			result.append("최소값은 1보다 큰 정수를 입력해주세요.\n");
			return;
		}
		
		primeList = new ArrayList<Boolean>(max+1);
		// 0번째와 1번째를 소수 아님으로 처리 
		primeList.add(false);					
		primeList.add(false);
		
		for(int i=2; i<=max; i++ ){
			primeList.add(i, true);
		}
		
		// 2 부터  ~ i*i <= n
		// 각각의 배수들을 지워간다.
		for(int i=2; (i*i)<=max; i++){
			if(primeList.get(i)){
				for(int j = i*i; j<=max; j+=i) primeList.set(j, false);
			}
		}
		
		
		//소수를 출력하는 문장
		//StringBuffer sb = new StringBuffer();
		//sb.append("{");
		//for(int i=0; i<=max; i++){
			//if(primeList.get(i) == true && i >=min){
			//	count++;
				//sb.append(i);
				//sb.append(",");				
			//}
		//}
		//sb.setCharAt(sb.length()-1, '}');
 
		//String str = new String(sb);
		//result.append(str);
												
		/*String end = in.format(new Date());*/
		
		etime = System.currentTimeMillis();
		result.append("걸린시간 : " + (etime-stime)/1000);
		
		//result.append("\n시작 : " + sta + "종료 : " + end + "(분:초)\n");
		//result.append("소수 개수 : " + count +"\n");
	}
	
	
	public void minor_tc(){ // 선생님 코드
		int min=0, max=0, cnt=0;
		long stime = 0;
		long etime = 0;
		result.setText("");
		boolean b = true; // 소수인 경우 true
		
		try{
			int s1 = Integer.parseInt(numIn1.getText());
			int s2 = Integer.parseInt(numIn2.getText());
			min = (s1 < s2)? s1 : s2;
			max = (s1 > s2)? s1 : s2;
			
			stime = System.currentTimeMillis();
			
			for (int k=min;k<=max;k++){
				int q = (int)Math.sqrt(k);
				b=true;
				if(k%2==0){
					b=false;					
				}else{
					for(int i=2;i<=q;i++){
						if(k%i==0){
							b=false;
							break;
						}
					}
				}				
				if(b){
					result.append(k + ",");
					cnt++;
				}
				
			}
				
			etime = System.currentTimeMillis();
			
			result.append("소수 개수 : " + cnt+"\n");
			result.append("경과 시간 : " + (etime-stime)/1000 + "초\n");
		}catch(Exception ex){
			JOptionPane.showMessageDialog(null, "정수형 숫자만 입력해라.");
		}		
	}
	public void test(){		
				
				long stime = 0;
				long etime = 0;
				stime = System.currentTimeMillis();
				
				min=Integer.parseInt(numIn1.getText());
				max=Integer.parseInt(numIn2.getText());
				
				boolean[] test = new boolean[max];
				test[0]=false;
				test[1]=false;
				for(int i=2; i<max; i++ ){
					test[i]=true;
				}
				for(int i=2; (i*i)<max; i++){
					if(test[i]){
						for(int j = i*i; j<max; j+=i) test[j]=false;
					}
				}			
				
				etime = System.currentTimeMillis();
				result.append("걸린시간 : " + (etime-stime));
				

	}
}
